package com.ai.academiainteligente;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.ai.WS.AIWSTask;
import com.ai.WS.AIWS;
import com.ai.util.AIActivity;

public class SignupActivity extends Activity implements OnClickListener,
		AIActivity {

	private InputMethodManager inMgr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signup);
		this.inMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		Button btnLogin = (Button) findViewById(R.id.btnOk);
		Button btnSignUp = (Button) findViewById(R.id.btnVoltar);
		btnLogin.setOnClickListener(this);
		btnSignUp.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.signup, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.btnVoltar) {
			finish();
		} else if (v.getId() == R.id.btnOk) {
			inMgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
			AIWSTask lookPartyTask = new AIWSTask(
					SignupActivity.this, AIWS.metodos.Singup);
			try {
				String nome = ((EditText) findViewById(R.id.edtNome)).getText()
						.toString();
				String senha = ((EditText) findViewById(R.id.edtSenha))
						.getText().toString();
				String email = ((EditText) findViewById(R.id.edtEmail))
						.getText().toString();
				String sexo = ((RadioButton) findViewById(((RadioGroup) findViewById(R.id.radioGroup1))
						.getCheckedRadioButtonId())).getTag().toString();
				lookPartyTask.execute(nome, email, senha, sexo);
			} catch (Exception e) {
				lookPartyTask.cancel(true);
			}
		}
	}

	@Override
	public void retornoWS(JSONObject json) {
		try {
			if (json.getInt("code") == 200) {
				finish();
			} else {
				Toast.makeText(this, "Problema", Toast.LENGTH_LONG).show();
			}
		} catch (JSONException e) {

		}
	}

	@Override
	public Activity getActivity() {
		return this;
	}

}
